import {getfileinfopage,getfileinfo} from "@/api/system"

// 详情不为空和字典转换
export const getNotNull = (val,dic,type) => {
  if(val){
    if(dic){
      var curCategory = dic.filter(
        item => item.value == val
      );
      if(curCategory && curCategory.length>0){
        return curCategory[0].label
      }
    }
    if(type == 'date'){
      return val.split('-')[0] + '年' + val.split('-')[1] + '月'
    }
    return htmlDecode(val)
  }
  if(type =='isOpen'){
    if(val=='1'){
      return '公开'
    }else if(val=='0'){
      return '非公开'
    }else if(val=='2'){
      return '部分公开'
    }
  }
  return htmlDecode('&nbsp;');
};

// 规划期限转换
export const dealStartEndDate = (startDate,endDate) => {
  if (startDate && endDate) {
    return startDate + '年至' + endDate + '年'
  }
  return htmlDecode('&nbsp;');
};
function htmlDecode(text){
  //1.首先动态创建一个容器标签元素，如DIV
  var temp = document.createElement("div");
  //2.然后将要转换的字符串设置为这个元素的innerHTML(ie，火狐，google都支持)
  temp.innerHTML = text;
  //3.最后返回这个元素的innerText(ie支持)或者textContent(火狐，google支持)，即得到经过HTML解码的字符串了。
  var output = temp.innerText || temp.textContent;
  temp = null;
  // return output.trim();
  return output.replace(/\s/g, "");
}

export const htmlDecodeex = (text) => {
  return htmlDecode(text);
};

export const getDetailData = (records) =>{
  var supportDeptName = '';
  if(records.supportDeptNameList && records.supportDeptNameList.length>0){
    for(var i in records.supportDeptNameList){
      supportDeptName += records.supportDeptNameList[i] + ','
    }
  }
  var detailData = [
    {label:'规划名称',value:getNotNull(records.title)},
    {label:'规划类型',value:getNotNull(records.category,PROJECT_DIC.CATEGORY),newline:true},
    {label:'规划期限',value: dealStartEndDate(records.startDate,records.endDate),newline:true},
    {label:'申报(牵头)单位',value:getNotNull(records.orgName),newline:true},
    {label:'协同编制部门',value:getNotNull(supportDeptName),newline:true},
    {label:'发布层级',value:getNotNull(records.planningLevel,PROJECT_DIC.PLANNING_LEVEL),newline:true},
    {label:'发布时间',value:getNotNull(records.releaseDate,null,'date'),newline:true},
    {label:'审批机关级别',value:getNotNull(records.approvingAuthorityLevel,PROJECT_DIC.APPROVING_AUTHORITY_LEVEL),newline:true},
    {label:'审批机关性质',value:getNotNull(records.approvingAuthorityNature,PROJECT_DIC.APPROVING_AUTHORITY_NATURE),newline:true},
    {label:'联系人姓名',value:getNotNull(records.contactName),newline:true},
    {label:'联系电话',value:getNotNull(records.contactNumber),newline:true},
    {label:'规划等级',value:getNotNull(records.planningRank,PROJECT_DIC.PLANNING_RANK)},
    {label:'编制经费来源',value:getNotNull(records.financialSource)},
    {label:'立项依据',value:getNotNull(records.basis),fileName:records.basisFileId,type:'basis'},
    // {label:'规划范围',value:getNotNull(records.scope)},

    {label:'主要内容',value:getNotNull(records.content)},
    {label:'相关材料',fileName:this.multiplefiles,type:'related'}
  ];
  return detailData;
};


/**
 *  多文件获取
 * @returns {PromiseLike<T | never> | Promise<T | never>}
 */
export async function getFileTypeFiles(id) {
  if(!id||id.length==0){
    return;
  }
  var ids = '';
  for(var i in id){
    ids += id[i] + ','
  }
  return new Promise((resolve, reject) => {
    getfileinfopage({fileIds:ids}).then(response => {
      var record = response.data.data.records;
      // 赋值前清零
      let fileTypeIds = [];
      for(var i in record){
        fileTypeIds.push({
          fileId:record[i].fileId,
          fileName:record[i].fileFullName
        })
      }
      resolve(fileTypeIds)
    })
  })
}


/**
 * 单文件获取
 * @param id
 * @returns {Promise<undefined|*>}
 */
export async function getFileTypeFile(id){
  if(!id){
    return;
  }
  return new Promise((resolve, reject) => {
    getfileinfo(id).then(response => {
      var record = response.data.data;
      let fileTypeId = [];
      fileTypeId.push({
        fileId:record.fileId,
        fileName:record.fileFullName
      });
      resolve(fileTypeId)
    })
  })
}

